package se.webbzon.oschi01.region;

import java.io.IOException;
import java.net.URL;

public class RegionReference {
	
	private final URL url;
	
	private final int x, z;
	
	public RegionReference(URL url) throws IOException {
		this.url = url;
		String[] parts = url.toString().split("\\.");
		if (parts.length < 3)
			throw new IOException("Could not read coordinate from region URL.");
		
		int x = 0, z = 0;
		for (int i = 2; i < parts.length; i++) {
			if (parts[i].toLowerCase().startsWith("mc")) try {
				x = Integer.parseInt(parts[i-2]);
				z = Integer.parseInt(parts[i-1]);
				break;
			} catch (NumberFormatException e) {
				throw new IOException(e);
			}
		}
		this.x = x;
		this.z = z;
	}
	
	public RegionReference(URL url, int x, int z) {
		this.url = url;
		this.x = x;
		this.z = z;
	}

	public URL getUrl() {
		return url;
	}

	public int getX() {
		return x;
	}

	public int getZ() {
		return z;
	}
	
	

}
